#include "ImageManager.hpp"
#include <iostream>

ImageManager::ImageManager()
{
    //ctor
}

ImageManager::~ImageManager()
{
    release();
}

sf::Sprite ImageManager::sprite(const std::string& strId)
{
    sf::Image* img = find(strId);

    if(img == 0)
    {
        img = load(strId);
    }

    if(img != 0)
    {
        m_img.insert(std::pair<std::string, sf::Image*>(strId, img));
        img->SetSmooth(false);
    }

    return sf::Sprite(*img);
}

sf::Image* ImageManager::find(const std::string& strId) const
{
    sf::Image* img(0);
    std::map<std::string, sf::Image*>::const_iterator it = m_img.find(strId);

    if(it != m_img.end())
    {
        img = it->second;
    }

    return img;
}


#include <iostream>
sf::Image* ImageManager::load(const std::string& strId)
{
    sf::Image* img = new sf::Image();

    if(!img->LoadFromFile(strId))
    {
        delete img;
        img = 0;
    }
    return img;
}

void ImageManager::release()
{
    while(m_img.begin() != m_img.end())
    {
        delete m_img.begin()->second;
        m_img.erase(m_img.begin());
    }
}

void ImageManager::release(const std::string& strId)
{
    sf::Image* img = find(strId);

    if(img != 0)
    {
        delete img;
        m_img.erase(m_img.find(strId));
    }
}
